<template>
  <div class="login">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form :rules="rules" :model="loginForm" ref="ruleFormRef">
          <h1>12138系统</h1>
          <el-form-item prop="username">
            <el-input
              :prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              :prefix-icon="Lock"
              v-model="loginForm.password"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login" :loading="loading"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>
<script setup lang="ts">
import { User, Lock } from "@element-plus/icons-vue";
import { reactive, ref } from "vue";
import { useRouter, useRoute } from "vue-router";
import useUserStore from "../../store/modules/user";
import { ElNotification } from "element-plus";
import getTime from "../../utils/time";
const loading = ref(false);
const router = useRouter();
const route = useRoute()
const useStore = useUserStore();
const loginForm = reactive({
  username: "admin",
  password: "111111",
});
const ruleFormRef = ref()
const validatorUsername = (_rule: any, value: any, callback: any) => {
  //rule规则对象
  //value文本内容
  //符合条件callback放行
  //不符合条件，callback注入错误提示信息
  if(value.length >= 5){
    callback()
  }else{
    callback(new Error('账号长度至少5位'))
  }
}
const validatorPassword = (_rule: any, value: any, callback: any) => {
  if(value.length >= 6){
    callback()
  }else{
    callback(new Error('密码长度至少6位'))
  }
}
const rules = reactive({
  username: [
    // {
    //   required: true,min:5,max:10, message: '用户名长度不小于5位', trigger: 'change'
    // }
    {
      trigger:'change',validator:validatorUsername
    }
  ],
  password: [
    // {
    //   required: true,min:6,max:15, message: '密码长度不小于6位', trigger: 'change'
    // }
    {
      trigger:'change',validator:validatorPassword
    }
  ],
});
const login = async () => {
  await ruleFormRef.value.validate();
  //加载开始
  loading.value = true;
  try {
    await useStore.useLogin(loginForm);
    const toPath:any = route.query.toPath
    router.push({ path: toPath || '/' });
    ElNotification({
      type: "success",
      message: "欢迎回来",
      title: `Hi,${getTime()}好`,
    });
    loading.value = false;
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
</script>
<style scoped lang="scss">
.login {
  width: 100%;
  height: 100vh;
  background: url("../../assets/images/background.jpg") no-repeat;
  background-size: cover;
  .el-form {
    h1 {
      color: white;
      font-size: 30px;
      padding-top: 20px;
      margin-bottom: 40px;
      text-align: center;
    }
    padding-left: 50px;
    padding-right: 50px;
    position: relative;
    top: 30vh;
    width: 80%;
    background: url("../../assets/images//login_form.png") no-repeat;
    background-size: cover;
    .el-form-item {
      padding: 5px;
      .el-button {
        width: 100%;
      }
    }
  }
}
</style>
